package com.disapand.strongarmy.extention

import com.disapand.strongarmy.vo.*
import org.ktorm.entity.*
import org.ktorm.schema.*
import kotlin.math.*

/**
 * ktorm 扩展函数
 * @auth disapand
 * @date 2021/11/11 - 11:52 - 星期四
 *
 */

/**
 * ktorm 分页扩展函数
 *
 * @param E
 * @param T
 * @param current
 * @param pageSize
 * @return
 */
fun <E : Any, T : BaseTable<E>> EntitySequence<E, T>.page(current: Int = 1, pageSize: Int = 20): PageVO<E> {
    val total = this.totalRecords
    val totalPage = ceil(total.toDouble() / pageSize).toInt()
    val list = this.drop((current - 1) * pageSize).take(pageSize).toList()
    return PageVO(
        total,
        totalPage,
        current,
        pageSize,
        list
    )
}